
package com.trydofor.id.xnet;

import java.util.List;

import com.trydofor.id.body.Bodies;
import com.trydofor.id.body.int64.Int64QueryBody;
import com.trydofor.id.body.int64.Int64ReplyBody;
import com.trydofor.id.byta.Bytable;
import com.trydofor.id.byta.Head;
import com.trydofor.id.byta.Packet;
import com.trydofor.id.mina.PacketDecoder;

/**
 * @author : Shi Rongjiu (www.trydofor.com)
 */
public abstract class Int64NetLoader<T extends Number> extends NetLoader<T> {
    
    private String maker;
    private int    count;
    
    /**
     * {@inheritDoc}
     */
    public Int64NetLoader(String host, int port, int auth) {
        super(host, port, auth);
    }
    
    /**
     * {@inheritDoc}
     */
    public Int64NetLoader(Int64NetLoader<T> loader) {
        super(loader);
        maker = loader.maker;
        count = loader.count;
    }
    
    @Override
    protected void registerDecoder(PacketDecoder pd) {
        pd.register(Bodies.INT64QUERY);
        pd.register(Bodies.INT64REPLY);
    }
    
    @Override
    protected Bytable<?> makeRequestBody(Head head) {
        return new Int64QueryBody(maker, count);
    }
    
    @Override
    protected void makeResultList(List<T> result, Packet reply) {
        Int64ReplyBody b = (Int64ReplyBody) reply.getBody();
        long[] ls = b.getLongs();
        for (int i = 0; i < ls.length; i++) {
            result.add(convert(ls[i]));
        }
    }
    
    /**
     * @return the maker
     */
    public String getMaker() {
        return maker;
    }
    
    /**
     * @param maker the maker to set
     */
    public void setMaker(String maker) {
        this.maker = maker;
    }
    
    /**
     * @return the count
     */
    public int getCount() {
        return count;
    }
    
    /**
     * @param count the count to set
     */
    public void setCount(int count) {
        this.count = count;
    }
    
    /**
     * convert long value to the Type
     * 
     * @param value the long
     * @return the Type
     */
    protected abstract T convert(long value);
}
